package com.windoent.yugioh.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnItemLongClickListener;
import com.windoent.yugioh.R;
import com.windoent.yugioh.adapter.ShareQqWechatAdapter;
import com.windoent.yugioh.bean.DrawaerLayoutListBean;
import com.windoent.yugioh.utils.TDevice;

import java.util.ArrayList;
import java.util.List;

import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * 作者：Android
 * 时间：2022/1/16 11:08
 * 功能：繼承Dialog，自定义更新Dialog
 */
public class ShareQqWechatDialog extends Dialog {

    private Context mContext;
    private RecyclerView mRecyclerView;

    private onOnclickListener onclickListener;//确定按钮被点击了的监听器


    public void setOnclickListener(onOnclickListener onclickListener) {
        this.onclickListener = onclickListener;
    }

    public ShareQqWechatDialog(Context context) {
        super(context, R.style.MoHUDialog);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.free_share_qq_wechat_dialog_layout);
        //按空白处能取消动画
        setCanceledOnTouchOutside(true);
        Window window = getWindow();
        window.setWindowAnimations(R.style.dialogBottomUpAnimation);
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.gravity = Gravity.CENTER;
        attributes.width = ViewGroup.LayoutParams.MATCH_PARENT;
        attributes.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setGravity(Gravity.BOTTOM);
        window.setAttributes(attributes);

        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();

    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {

    }

    /**
     * 初始化界面控件的显示数据
     */
    private void initData() {
        //如果用户自定了title和message
        initAdapter();
    }

    private void initAdapter() {
        ShareQqWechatAdapter adapter = new ShareQqWechatAdapter();
        mRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        adapter.setNewInstance(getRvData());
        mRecyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> baseQuickAdapter, @NonNull View view, int position) {
                String shareName;
                switch (position) {
                    case 0:
                        //qq空间
                        shareName = QZone.NAME;
                        break;
                    case 1:
                        //朋友圈
                        shareName = WechatMoments.NAME;
                        break;
                    case 2:
                        //微信好友
                        shareName = Wechat.NAME;
                        break;
                    case 3:
                        //qq好友
                        shareName = QQ.NAME;
                        break;
                    default:
                        throw new IllegalStateException("Unexpected value: " + position);
                }
                if (onclickListener != null) {
                    onclickListener.onClick(shareName);
                    dismiss();
                }
            }
        });
    }

    private List<DrawaerLayoutListBean> getRvData() {
        List<DrawaerLayoutListBean> list = new ArrayList<>();
        list.clear();
        list.add(new DrawaerLayoutListBean(R.drawable.drawable_share_qq_zone, mContext.getString(R.string.ssdk_qzone)));
        list.add(new DrawaerLayoutListBean(R.drawable.drawable_dialog_wechat_moment, mContext.getString(R.string.ssdk_wechatmoments)));
        list.add(new DrawaerLayoutListBean(R.drawable.drawable_dialog_wechat_share, mContext.getString(R.string.ssdk_wechat)));
        list.add(new DrawaerLayoutListBean(R.drawable.drawable_share_dialog_qq, mContext.getString(R.string.ssdk_qq)));
        return list;
    }

    /**
     * 初始化界面控件
     */
    private void initView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    }

    public interface onOnclickListener {
        public void onClick(String name);
    }

}
